<template>
  <div class="Login-bgc">
    <div class="Login-div">
      <!-- 头像区域 -->
      <div class="Login-img">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 带验证的表单区域 -->
      <el-form
        :model="LoginForm"
        :rules="LoginRules"
        ref="LoginFormRef"
        label-width="100px"
        class="Login-form"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            v-model="LoginForm.username"
            class="Login-name"
            prefix-icon="el-icon-user-solid"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="LoginForm.password"
            class="Login-password"
            prefix-icon="el-icon-lock"
            type="password"
          ></el-input>
        </el-form-item>
        <!-- 登录重置按钮 -->
        <el-form-item class="Login-btn">
          <el-button type="primary" @click="getLogin">登录</el-button>
          <el-button type="info" @click="LoginReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 登录表单的绑定对象
      LoginForm: {
        username: "admin",
        password: "123456",
      },
      // 登录表单的验证规则对象
      LoginRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 5, max: 10, message: "长度在 5到 10 个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    // 重置表单
    LoginReset() {
      this.$refs.LoginFormRef.resetFields();
    },
    // 登录前表单验证及向后台传递用户信息登录
    getLogin() {
      this.$refs.LoginFormRef.validate(async (valid) => {
        if (!valid) {
          return;
        } else {
          const { data: res } = await this.$http.post("login", this.LoginForm);
          console.log(res);
          if (res.meta.status !== 200) {
            return this.$message.error("登录失败！");
          } else {
            this.$message.success("登录成功");
            window.sessionStorage.setItem("token", res.data.token);
            this.$router.push("/home");
          }
        }
      });
    },
  },
};
</script>


<style  scoped>
.login-bgc {
  background-color: pink;
  height: 100%;
  width: 100%;
}
.Login-div {
  width: 450px;
  height: 300px;
  background-color: pink;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

img {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: pink;
}
.Login-form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

.Login-name {
  width: 415px;
  margin-left: -100px;
}
.Login-password {
  width: 415px;
  margin-left: -100px;
}
.Login-btn {
  display: flex;
  justify-content: flex-end;
}
</style>